package com.qianfeng.controller;

import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Department;
import com.qianfeng.entity.Role;
import com.qianfeng.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@ResponseBody
@Controller
@CrossOrigin
@RequestMapping(value = "/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @RequestMapping(value = "/addDept")
    public Result addDept(@RequestBody Department department){
        departmentService.addDept(department);
        Result result = new Result();
        return result;
    }

    /**
     * 删除角色
     * @param deptId
     * @return
     */
    @RequestMapping(value = "/deleteDept")
    public Result deleteDept(Integer deptId){
        departmentService.deleteDept(deptId);
        Result result = new Result();
        return result;
    }

    /**
     * 更新角色
     * @param department
     * @return
     */
    @RequestMapping(value = "/updateDept")
    public Result updateDept(@RequestBody Department department){
        departmentService.updateDept(department);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/getDeptPage")
    public Result getDeptPage(Integer pageNo,Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        Page page = departmentService.getDeptPage(pageNo,pageSize);
        Result result = new Result();
        result.setData(page);
        return result;

    }

    @RequestMapping(value = "/getDeptList")
    public Result getDeptList(){
        List<Department> deptList = departmentService.getDeptList();
        Result result = new Result();
        result.setData(deptList);
        return result;
    }

    @RequestMapping(value = "/getDeptByDeptId")
    public Result getDeptByDeptId(Integer deptId){
        List<Department> deptByDeptId = departmentService.getDeptByDeptId(deptId);
        Result result = new Result();
        result.setData(deptByDeptId);
        return result;
    }


}
